<script setup>
import { ref } from 'vue'
import {Download, Plus, Upload} from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import { http } from "@/api";
import {useRouter} from "vue-router";
import {sentimentTemplateDownloadUrl} from "@/utils/common";
import {ElMessage} from "element-plus";
const router = useRouter()
/**
 * 搜索
 */

const dateRange = ref([])
const changeDate = (val) => {
  if (!val) {
    searchParams.value.startCreate = ''
    searchParams.value.endCreate = ''
    return
  }
  searchParams.value.startCreate = val[0] || ''
  searchParams.value.endCreate = val[1] || ''
}
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.Tender,
  routerPre: 'Tender',
  params: {
    "endCreate": "",
    "startCreate": "",
    "title": ""
  },
})
const updateTableRow = (row) => {
  http.Tender.updateIndex({
    id: row.id,
    sort: row.sort
  }).then(res => {
    ElMessage.success('排序成功')
    search()
  })
}
const rankTop = (row) => {
  http.Tender.updateTop({
    id: row.id,
    asterisk: row.asterisk === 1 ? 0 : 1
  }).then(res => {
    ElMessage.success('操作成功')
    search()
  })
}
const changeStatus = (row) => {
  http.Tender.updateStatus({
    id: row.id,
    status: row.status
  }).then(res => {
    ElMessage.success('操作成功')
  }).catch(() => {
    if (row.status === 1) {
      row.status = 0
    } else {
      row.status = 1
    }
  })
}

</script>


<template>
  <PageMain title="招投标">
    <el-form label-suffix="：" inline>
      <el-form-item label="文章标题">
        <el-input clearable v-model="searchParams.title"></el-input>
      </el-form-item>

      <el-form-item label="发布时间">
        <el-date-picker clearable
            @change="changeDate"
            v-model="dateRange"
            type="daterange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="文章标题" min-width="300px" property="title"></el-table-column>
        <el-table-column label="投标时间" prop="time"/>
        <el-table-column label="图片">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <ViewImage type="image" :image-url="scope.row.imageUrl"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="排序" min-width="80px">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-input placeholder="请输入排序" v-model="scope.row.sort" type="number" @blur="updateTableRow(scope.row)"/>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" width="180px" prop="createTime"></el-table-column>
        <el-table-column label="操作" fixed="right" width="220px">
          <template #default="scope">
            <el-button type="text" @click="rankTop(scope.row)">{{ scope.row.asterisk === 1 ? '取消置顶' : '置顶'}}</el-button>
            <el-button type="text" @click="openView(scope.row)">查看</el-button>
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
